package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.smart.village.big.screen.domain.*;
import com.smart.village.big.screen.mapper.ChangeGovernanceMapper;
import com.smart.village.big.screen.mapper.GridInformationMapper;
import com.smart.village.big.screen.service.ChangeGovernanceService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import static com.smart.village.big.screen.utils.PointInPolygonChecker.isPointInPolygon;
import static org.apache.tomcat.util.file.Matcher.match;


@Service
public class ChangeGovernanceServiceImpl implements ChangeGovernanceService {
    @Autowired
    private ChangeGovernanceMapper changeGovernanceMapper;
    @Autowired
    private GridInformationMapper gridInformationMapper;


    @Override
    public JSONObject getTeSeChanYeList(String townNumber, String villageNumber, String tab, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String tagSerialNumber = "";
        if(StringUtil.isBlank(tab)){
            tagSerialNumber = "NRT000000000008";
        }
        switch (tab){
            case "定制农业板块":
                tagSerialNumber = "NRT000000000006";
                break;
            case "特色种植":
                tagSerialNumber = "NRT000000000007";
                break;
            case "我在北极有块田":
                tagSerialNumber = "NRT000000000008";
                break;
        }
        List<JSONObject> personalPointsAccountList = changeGovernanceMapper.getTeSeChanYeList(townNumber, villageNumber, tagSerialNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(personalPointsAccountList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getTeSeChanYeDetail(String tab,String naturalResourceSerialNumber) {

        JSONObject teSeChanYeDetail = changeGovernanceMapper.getTeSeChanYeDetail(naturalResourceSerialNumber);
        //todo 有块田的单独出 【种植技术支持，生长情况监控，土壤监控状况】
        if("我在北极有块田".equals(tab)){
            JSONObject jsonObject = new JSONObject();
            if(StringUtils.isNotEmpty(teSeChanYeDetail.getString("Land_Expert_SerialNumber"))){
             JSONObject Planting_Experience = changeGovernanceMapper.getLandExpertBySerialNumber(teSeChanYeDetail.getString("Land_Expert_SerialNumber"));
                jsonObject.put("Planting_Experience",Planting_Experience);
            }
            jsonObject.put("Growth_Conditions_Monitoring",new JSONObject());
            jsonObject.put("Soil_Monitoring_Status",new JSONObject());
            teSeChanYeDetail.put("detail",jsonObject);
        }
        return teSeChanYeDetail;
    }

    @Override
    public JSONObject getCunSheZhiLiMap(String townNumber, String villageNumber, String bluePlateTypeSerialNumber) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "FeatureCollection");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        JSONObject crsP = new JSONObject();
        crsP.put("name", "EPSG:4490");
        crs.put("properties", crsP);

        jsonObject.put("crs", crs);
        Set<JSONObject> list = Sets.newHashSet();
        // 所有蓝牌标签
        if(StringUtils.isBlank(villageNumber)){
             list.addAll(changeGovernanceMapper.getCunSheZhiLiMap(townNumber, villageNumber, bluePlateTypeSerialNumber));
        }else{
            // 1 获取该村边界坐标集合
             List<JSONObject> boundaryCoords = changeGovernanceMapper.getVillageBoundaryCoords(townNumber, villageNumber);
             List<JSONObject>   allTownList =    changeGovernanceMapper.getCunSheZhiLiMap(townNumber, "", bluePlateTypeSerialNumber);
             if(!CollectionUtils.isEmpty(boundaryCoords)){


                 List<Point> points = Lists.newArrayList();
                 // 2 遍历所有该镇的蓝牌，判断是否在该村边界内 返回
                 for (JSONObject boundaryCoord : boundaryCoords) {

                     JSONArray coords = boundaryCoord.getJSONArray("Coords");
                     coords.forEach(coord -> {
                         JSONArray parse = JSONArray.parseArray(coord.toString());
                         for (Object p : parse) {
                             String o = p.toString();
                             String[] split = o.replace(" ","").replace("[","").replace("]","").split(",");
                             Point pointObj = new Point(Double.valueOf(split[0]), Double.valueOf(split[1]));
                             points.add(pointObj);
                         }
                     });
                     Polygon polygon = new Polygon(boundaryCoord.getString("DistrictNumber"),points);
                     for (JSONObject bluePlate : allTownList) {
                         String point = bluePlate.getString("Coord").replace(" ", "").replace("[","").replace("]","");
                         if(StringUtils.isBlank(point)){
                             continue;
                         }
                         Point pointObj = new Point(Double.valueOf(point.split(",")[0]), Double.valueOf(point.split(",")[1]));

                         if(isPointInPolygon(pointObj,polygon)){
                             list.add(bluePlate);
                         }
                     }
                 }

             }

        }

        List<JSONObject> features = Lists.newArrayList();
        for (JSONObject coords : list) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            feature.put("id", coords.getString("Blue_Plate_SerialNumber"));
            JSONObject geometry = new JSONObject();
            geometry.put("type", "point");
            geometry.put("coordinates", coords.getString("Coord").replace(" ", ""));
            feature.put("geometry", geometry);
            feature.put("properties", coords);
            if (!ObjectUtils.isEmpty(coords.getString("Coord")) && !coords.getString("Coord").replace(" ", "").equals("[]")) {
                coords.remove("Coords");
                coords.remove("Coord");
                features.add(feature);
            }
        }
        jsonObject.put("features", features);
        return jsonObject;
    }

    @Override
    public JSONObject getCunSheZhiLiMapDetail(String bluePlateSerialNumber) throws BadHanyuPinyinOutputFormatCombination {
        JSONObject cunSheZhiLiMapDetail = changeGovernanceMapper.getCunSheZhiLiMapDetail(bluePlateSerialNumber);
        String bluePlateName = cunSheZhiLiMapDetail.getString("Blue_Plate_Name");

        cunSheZhiLiMapDetail.put("Blue_Plate_Name_Pinyin",convertToUpperCasePinyin(bluePlateName));
        return cunSheZhiLiMapDetail;
    }

        public static String convertToUpperCasePinyin(String chinese) throws BadHanyuPinyinOutputFormatCombination {
            StringBuilder pinyinBuilder = new StringBuilder();

            char[] chars = chinese.toCharArray();
            HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
            outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (char character : chars) {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(character,outputFormat);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    // 获取第一个拼音，并转换为大写
                    String pinyin = pinyinArray[0].toUpperCase();
                    pinyinBuilder.append(pinyin).append(" ");
                } else {
                    // 对于没有拼音的字符，直接添加原字符
                    pinyinBuilder.append(character).append(" ");
                }
            }

            return pinyinBuilder.toString().trim();
        }


    @Override
    public JSONObject getComprehensiveGridManageStatic(String townNumber, String villageNumber) {
        List<GridInformation> gridInformation =
                changeGovernanceMapper.getComprehensiveGridManageStatic(townNumber, villageNumber);

        int zhuanYeCnt = 0;
        int jianZhiCnt = 0;
        int zhuanZhiCnt = 0;
        for (GridInformation gi : gridInformation) {
            GridMember gm = new GridMember();
            gm.setGridSerialNumber(gi.getGridSerialNumber());
            gm.setRole("2");
            List<GridMember> role2List = changeGovernanceMapper.getGridMemberList(gm);
            zhuanZhiCnt+= role2List.size();

            gm.setRole("3");
            List<GridMember> role3List = changeGovernanceMapper.getGridMemberList(gm);
            jianZhiCnt += role3List.size();

            gm.setRole("4");
            List<GridMember> role4List = changeGovernanceMapper.getGridMemberList(gm);
            zhuanYeCnt += role4List.size();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("zhuanYeCnt", zhuanYeCnt);
        jsonObject.put("jianZhiCnt", jianZhiCnt);
        jsonObject.put("zhuanZhiCnt", zhuanZhiCnt);
        jsonObject.put("wangGeCnt", gridInformation.size());

        return jsonObject;
    }

    @Override
    public JSONObject getInspectionVisitSituationList(String townNumber, String villageNumber, String type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> list = changeGovernanceMapper.getInspectionVisitSituationList(townNumber, villageNumber, type);
        if("事件处理".equals(type)){
            double totalCategoryId = 0;
            for (JSONObject jsonObject : list) {
                totalCategoryId += jsonObject.getInteger("category_id");
            }
            for (int i = 0; i < list.size(); i++) {
                JSONObject jsonObject = list.get(i);
                double ratioDouble = jsonObject.getDouble("category_id") / totalCategoryId;
                BigDecimal rounded = new BigDecimal(ratioDouble).setScale(2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
                String result = rounded.toPlainString();
                if (result.endsWith(".00")) {
                    result = result.substring(0, result.length() - 3); // 去掉末尾的".00"
                }
                jsonObject.put("ratio", result+"%");
                jsonObject.put("rank",i+1);
            }
        }

        PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }


    @Override
    public JSONObject getPatrolCheckInSubTaskList(String taskPlanSerialNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> list = changeGovernanceMapper.getPatrolResultMainGridList(taskPlanSerialNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }
    @Override
    public List<JSONObject> getPatrolResultDetailGridList(String resultMainSerialNumber) {
        return changeGovernanceMapper.getPatrolResultDetailGridList(resultMainSerialNumber);
    }


    @Override
    public JSONObject getEnvironment1(String townNumber, String villageNumber) {
        //todo 人居环境1 和 2 是假数据 等物联网接入
        JSONObject jsonObject =
                changeGovernanceMapper.getEnvironment1(townNumber, villageNumber);
        return jsonObject;
    }

    @Override
    public JSONObject getEnvironment1Detail(String townNumber, String villageNumber, String type) {
        JSONObject jsonObject =
                changeGovernanceMapper.getEnvironment1Detail(townNumber, villageNumber,type);
        return jsonObject;
    }

    @Override
    public JSONObject getEnvironment2(String townNumber, String villageNumber, String status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<JSONObject> list = changeGovernanceMapper.getEnvironment2(townNumber, villageNumber, status);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public List<JSONObject> getCunSheZhiLiMapToolLips(String parentSerialNumber) {
        return changeGovernanceMapper.getCunSheZhiLiMapToolLips(parentSerialNumber);
    }

    @Override
    public List<JSONObject> getDigitalDoorplateStaticsTagList() {
        return changeGovernanceMapper.getDigitalDoorplateStaticsTagList();
    }

    @Override
    public List<JSONObject> getDigitalDoorplateStatics(String townNumber, String villageNumber, String tagSerialNumber) {
        return changeGovernanceMapper.getDigitalDoorplateStatics(townNumber,villageNumber,tagSerialNumber);

    }

    @Override
    public List<JSONObject> getPatrolCheckIn(String townNumber, String villageNumber) {
        return changeGovernanceMapper.getPatrolCheckIn(townNumber, villageNumber);

    }

    @Override
    public JSONObject getVillageBaseInformation(String townNumber, String villageNumber) {
        JSONObject districtIntroduction = changeGovernanceMapper.getDistrictIntroduction(townNumber, villageNumber);

        JSONObject villageBaseInformation = changeGovernanceMapper.getVillageBaseInformation(townNumber, villageNumber);
        JSONObject flowPersonInformation = changeGovernanceMapper.getFlowPersonInformation(townNumber, villageNumber);
        if(ObjectUtils.isEmpty(flowPersonInformation)){
            flowPersonInformation.put("flowPersonCnt", 0);
        }else{
            villageBaseInformation.put("flowPersonCnt", flowPersonInformation.getInteger("flowPersonCnt"));
        }

        if(ObjectUtils.isEmpty(districtIntroduction)){
            villageBaseInformation.put("gridCnt", 0);
            villageBaseInformation.put("domicileCnt", 0);
            villageBaseInformation.put("area", 0);
            villageBaseInformation.put("collectiveCnt", 0);
            villageBaseInformation.put("cultivatedArea", 0);
            villageBaseInformation.put("waterArea", 0);
            villageBaseInformation.put("personCnt", 0);
            return villageBaseInformation;
        }else {
            if(!villageBaseInformation.containsKey("gridCnt")){
                villageBaseInformation.put("gridCnt", 0);
            }
            if(!villageBaseInformation.containsKey("domicileCnt")){
                villageBaseInformation.put("domicileCnt", 0);
            }
            if(!villageBaseInformation.containsKey("area")){
                villageBaseInformation.put("area", 0);
            }
            if(!villageBaseInformation.containsKey("collectiveCnt")){
                villageBaseInformation.put("collectiveCnt", 0);
            }
            if(!villageBaseInformation.containsKey("cultivatedArea")){
                villageBaseInformation.put("cultivatedArea", 0);
            }
            if(!villageBaseInformation.containsKey("waterArea")){
                villageBaseInformation.put("waterArea", 0);
            }
            if(!villageBaseInformation.containsKey("personCnt")){
                villageBaseInformation.put("personCnt", 0);
            }
            districtIntroduction.forEach((key, value) -> {
                villageBaseInformation.put(key, value);
            });

            List<GridInformation> gridInformation =
                    changeGovernanceMapper.getGridInformationByVillage(townNumber, villageNumber);
            if (CollectionUtils.isEmpty(gridInformation)) {
                villageBaseInformation.put("gridCnt", 0);
            } else {
                villageBaseInformation.put("gridCnt", gridInformation.size());
            }
            return villageBaseInformation;
        }
    }

    @Override
    public JSONObject getInspectionVisitSituation(String townNumber, String villageNumber) {
        JSONObject jsonObject = new JSONObject();
        String Focus = "0";
        String Grid_Member = "0";
        String Event_Deal = "0";
        String Visit_Rate = "0";

        Focus = changeGovernanceMapper.getInspectionVisitSituationFocus(townNumber,villageNumber);
        Grid_Member = changeGovernanceMapper.getInspectionVisitSituationGridMember(townNumber,villageNumber);
        Event_Deal = changeGovernanceMapper.getInspectionVisitSituationEventDeal(townNumber,villageNumber);
        Visit_Rate = changeGovernanceMapper.getInspectionVisitSituationVisitRate(townNumber,villageNumber);

        jsonObject.put("Focus", Focus);
        jsonObject.put("Grid_Member", Grid_Member);
        jsonObject.put("Event_Deal", Event_Deal);
        jsonObject.put("Visit_Rate", StringUtils.isEmpty(Visit_Rate)?"0":new BigDecimal(Visit_Rate).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString());

        return jsonObject;
    }

    @Override
    public JSONObject getVillagerCoGovernance(String townNumber, String villageNumber) {
        JSONObject result = new JSONObject();

        List<JSONObject> villagerCoGovernance = changeGovernanceMapper.getVillagerCoGovernance(townNumber, villageNumber);
        villagerCoGovernance.forEach(jsonObject -> {
            if (jsonObject.getString("type").equals("今日上报")) {
                result.put("upCnt",jsonObject.getInteger("cnt"));
            }
            if (jsonObject.getString("type").equals("今日处理")) {
                result.put("doneCnt",jsonObject.getInteger("cnt"));
            }
            if (jsonObject.getString("type").equals("待处理")) {
                result.put("willCnt",jsonObject.getInteger("cnt"));
            }

        });
        if(result.containsKey("upCnt")&&result.getInteger("upCnt")==0){
            result.put("finishPercent",0);
        }
        if(result.containsKey("doneCnt")&&result.getInteger("doneCnt")==0){
            result.put("finishPercent",0);
        }
        if(result.containsKey("upCnt")&&result.getInteger("upCnt")!=0){
            Integer doneCnt = result.getInteger("doneCnt");
            Integer upCnt = result.getInteger("upCnt");
            double v = doneCnt.doubleValue() / upCnt.doubleValue();
            DecimalFormat df = new DecimalFormat("#.00");
            String str = df.format(v*100);
            result.put("finishPercent",new BigDecimal(str));
        }
        return result;
    }

    @Override
    public List<JSONObject> getVillagerCoGovernance2(String townNumber, String villageNumber, String searchType) {
        if( StringUtils.isEmpty(searchType)) {
            return Lists.newArrayList();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        switch (searchType) {
            case "threeYear" -> {
                calendar.add(Calendar.YEAR, -3);
                Date timeThreeYearAgo = calendar.getTime();
                String yearThreeYearAgo = DateFormatUtils.format(timeThreeYearAgo, "yyyy");
                String nowYear = DateFormatUtils.format(new Date(), "yyyy");
                return changeGovernanceMapper.getVillagerCoGovernance2(townNumber, villageNumber, searchType, yearThreeYearAgo, nowYear);
            }
            case "currentWeek" -> {
                LocalDate today = LocalDate.now();
                LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                DateTimeFormatter simpleDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String currentWeekFirst =   firstDayOfWeek.format(simpleDateFormat);
                String currentNow = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
                return changeGovernanceMapper.getVillagerCoGovernance2(townNumber, villageNumber, searchType, currentWeekFirst, currentNow);
            }
            case "currentMonth" -> {
                String currentMonth = DateFormatUtils.format(new Date(), "yyyy-MM");
                return changeGovernanceMapper.getVillagerCoGovernance2(townNumber, villageNumber, searchType, currentMonth, currentMonth);
            }
        }
        return Lists.newArrayList();
    }

    @Override
    public JSONObject getGridInformationCoordsByVillage(String townNumber, String villageNumber) {
        List<GridInformation> gridInformation =
                changeGovernanceMapper.getGridInformationByVillage(townNumber, villageNumber);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "FeatureCollection");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        JSONObject properties1 = new JSONObject();
        properties1.put("name", "EPSG:4490");
        crs.put("properties", properties1);
        jsonObject.put("crs", crs);
        List<JSONObject> features = Lists.newArrayList();
        gridInformation.forEach(gridInformation1 -> {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            feature.put("id", gridInformation1.getGridSerialNumber());
            JSONObject geometry = new JSONObject();
            geometry.put("type", "Polygon");
            geometry.put("coordinates", gridInformation1.getCoords());
            feature.put("geometry", geometry);
            JSONObject properties = new JSONObject();
            properties.put("FID", gridInformation1.getGridSerialNumber());
            properties.put("id", gridInformation1.getGridSerialNumber());
            String gridName = gridInformation1.getGridName();
            properties.put("name", gridName);
            GridMember gm = new GridMember();
            gm.setGridSerialNumber(gridInformation1.getGridSerialNumber());

            List<GridMember> gridMemberList = changeGovernanceMapper.getGridMemberList(gm);
            List<JSONObject> memberJsonList = Lists.newArrayList();
            gridMemberList.forEach(gridMember -> {
                JSONObject memberJson = new JSONObject();
                memberJson.put("role", gridMember.getRole());
                memberJson.put("roleName", gridMember.getRoleName());
                memberJson.put("personalName", gridMember.getPersonalName() == null ? "" : gridMember.getPersonalName());
                memberJson.put("avatar", gridMember.getAvatar() == null ? "" : gridMember.getAvatar());
                memberJson.put("contactPhone", gridMember.getContactPhone() == null ? "" : gridMember.getContactPhone());
                memberJsonList.add(memberJson);
            });
            properties.put("gridMemberList", memberJsonList);
            feature.put("properties", properties);
            features.add(feature);
        });
        jsonObject.put("features", features);
        return jsonObject;
    }

    @Override
    public List<JSONObject> getGridMemberByGridSerialNumber(String gridSerialNumber,String role) {
        GridMember gm = new GridMember();
        gm.setGridSerialNumber(gridSerialNumber);
        if(StringUtils.isNotEmpty(role)){
            gm.setRole(role);
        }
        List<GridMember> gridMemberList = changeGovernanceMapper.getGridMemberList(gm);
        List<JSONObject> memberJsonList = Lists.newArrayList();
        gridMemberList.forEach(gridMember -> {
            JSONObject memberJson = new JSONObject();
            memberJson.put("role", gridMember.getRole());
            memberJson.put("iDCardNumber", gridMember.getiDCardNumber());
            memberJson.put("village", gridMember.getVillage());
            memberJson.put("gridName", gridMember.getGridName());
            memberJson.put("gridSerialNumber", gridMember.getGridSerialNumber());
            memberJson.put("roleName", gridMember.getRoleName());
            memberJson.put("personalName", gridMember.getPersonalName() == null ? "" : gridMember.getPersonalName());
            memberJson.put("avatar", gridMember.getAvatar() == null ? "" : gridMember.getAvatar());
            memberJson.put("contactPhone", gridMember.getContactPhone() == null ? "" : gridMember.getContactPhone());
            memberJsonList.add(memberJson);
        });
            return memberJsonList;
    }

    @Override
    public JSONObject getCunSheZhiLiImportantPersonMap(String townNumber, String villageNumber,String personalTagSerialNumber) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "FeatureCollection");
        JSONObject crs = new JSONObject();
        crs.put("type", "name");
        JSONObject crsP = new JSONObject();
        crsP.put("name", "EPSG:4490");
        crs.put("properties", crsP);

        jsonObject.put("crs", crs);
        List<JSONObject> list = changeGovernanceMapper.getCunSheZhiLiImportantPersonList(townNumber,villageNumber,personalTagSerialNumber);
        List<JSONObject> features = Lists.newArrayList();
        for (JSONObject coords : list) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");
            feature.put("id", coords.getString("Personal_Mapping_Tag_SerialNumber"));
            JSONObject geometry = new JSONObject();
            geometry.put("type", "point");
            geometry.put("coordinates", coords.getString("Coord").replace(" ", ""));
            feature.put("geometry", geometry);
            feature.put("properties", coords);
            if (!ObjectUtils.isEmpty(coords.getString("Coord")) && !coords.getString("Coord").replace(" ", "").equals("[]]")) {
                coords.remove("Coords");
                coords.remove("Coord");
                features.add(feature);
            }
        }
        jsonObject.put("features", features);
        return jsonObject;
    }

    @Override
    public List<JSONObject> getCunSheZhiLiImportantPersonMapToolLips() {
        return changeGovernanceMapper.getCunSheZhiLiImportantPersonMapToolLips();
    }

    @Override
    public List<JSONObject> getGridInformationToolLipByVillage(String townNumber, String villageNumber) {
        return changeGovernanceMapper.getGridInformationToolLipByVillage(townNumber, villageNumber);
    }

    @Override
    public JSONObject getGridEventListByExecReceiver(String iDCardNumber,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> list = changeGovernanceMapper.getGridEventListByExecReceiver(iDCardNumber);
        PageInfo<JSONObject> pageInfo = new PageInfo<>(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;

    }

    @Override
    public JSONObject getComplaintsDetail(String complaintId) {
        JSONObject complaintsDetail = gridInformationMapper.getComplaintsDetail(complaintId);
        if(ObjectUtils.isEmpty(complaintsDetail)){
            return new JSONObject();
        }
        return complaintsDetail;
    }
}
